package org.example.repository;

import org.example.entity.Scene;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 场景数据访问接口
 */
@Repository
public interface SceneRepository extends JpaRepository<Scene, Long> {
    
    /**
     * 根据用户ID查找场景
     */
    List<Scene> findByUserId(Long userId);
    
    /**
     * 根据用户ID和场景名称查找场景
     */
    Optional<Scene> findByUserIdAndName(Long userId, String name);
    
    /**
     * 根据激活状态查找场景
     */
    List<Scene> findByUserIdAndIsActive(Long userId, Integer isActive);
    
    /**
     * 根据场景名称模糊查询
     */
    @Query("SELECT s FROM Scene s WHERE s.userId = :userId AND s.name LIKE %:name%")
    List<Scene> findByUserIdAndNameContaining(@Param("userId") Long userId, @Param("name") String name);
    
    /**
     * 统计用户场景总数
     */
    @Query("SELECT COUNT(s) FROM Scene s WHERE s.userId = :userId")
    long countByUserId(@Param("userId") Long userId);
    
    /**
     * 统计激活场景数量
     */
    @Query("SELECT COUNT(s) FROM Scene s WHERE s.userId = :userId AND s.isActive = 1")
    long countActiveScenesByUserId(@Param("userId") Long userId);
    
    /**
     * 根据状态统计场景数量
     */
    @Query("SELECT COUNT(s) FROM Scene s WHERE s.isActive = :status")
    long countByStatus(@Param("status") Integer status);
}
